#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <Python.h>

#include "changesettings.h"
#include "magnifierwindow.h"
#include "showtextwindow.h"
#include "texttospeech.h"
#include "cutimage.h"
#include "aliyuntts.h"
#include "aidialog.h"
#include "customdialog.h"
#include "audiologo.h"

#include <QMainWindow>
#include <QDir>
#include <QPushButton>
#include <QListWidget>
#include <QListWidgetItem>
#include <QKeyEvent>
#include <QLabel>
#include <QEvent>
#include <QThreadPool>
#include <QRunnable>
#include <QTimer>
#include <QProcess>
#include <QVector>
#include <QCursor>
#include <QHotkey>
#include <QPainter>
#include <QLinearGradient>
#include <QTime>
#include <QFile>
#include <QTextStream>
#include <QMediaPlayer>
#include <QAudioRecorder>


#define TITLE "麒麟颂语"

/*class TextToSpeechTask : public QRunnable
{
public:
    TextToSpeechTask(const QString& text);

    void run() override;

private:
    QString m_text;
};*/

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    // 导入python模块以及方法
    void importPythonModules();
    void importPythonFunctions();

    void installFilter();

    // 五种发音引擎
    void textChange(QString text);
    void microsoftSpeak();
    void googleSpeak(QString text);
    void pyttsSpeak(QString text);
    /**
     * (type) 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，
     * 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美
     * (rate) 语速，取值0-15，默认为5中语速
     * (pitch ) 音调，取值0-15，默认为5中语调
     * (volume) 音量，取值0-9，默认为5中音量
     * (format) 下载的文件格式, 3：mp3(default) 4：pcm-16k 5：pcm-8k 6: wav
     */
    void baiduSpeak(QString text, QString type="0", QString rate="5", QString pitch="5", QString volume="5", QString format="3");
    void aliyunSpeak(QString text);
    // 更具用户的选择来调用相对应的发音引擎
    void speakText(QString text);

    // 界面的初始化
    void initializeWindow();
    // 获取系统支持的声音引擎，初始化pyttsx3
    void setPyttsx3ComboBox();
    // 将所有的槽函数连接操作汇总
    void connectSlots();

    // style beautify--给界面中的控件添加图标
    void playAudio();
    void showTextBox();
    void showBigTextBox();
    void menuIcon();
    void windowBeautify();

    // extra-functions
    // 语音合成
    void generateAudio(QString text, QString path);
    // 网址导航
    void transToUrl(QString url);
    // 一键复制
    void copyText(QString text);
    // 用来更新首页中语速，音高和音量标签的值
    void updateLabelValue();
    // 获取每个组件，放大
    void getAllWidgets(QWidget *widget, float zoomFactor);


private slots:
    void on_menuList_currentRowChanged(int currentRow);

    //void onmenulistItem(QListWidgetItem *item);

    void menulistItemChanged(QListWidgetItem *current, QListWidgetItem *previous);

    void on_btnCancel_clicked();

    void on_btnStart_clicked();

    void updateratevalue(int);

    void updatepitchvalue(int);

    void updatezoomvalue(int);

    void onvoicechange(int value);

    //tts
    void setRate(int);

    void setPitch(int);

    //two timer
    void mouseInText();

    void handleImgChange();

    void magnifierWindowSlot();  //放大镜

    void normalizeWindow(); //窗口正常化

    void hotkeyStart();  //开始使用

    void hotkeyQuit();  //退出使用

    void showTextWindowSlots();   //显示文本框

    void copyNowText();  //复制当前的朗读文本

    void hotkeyAi();    //快捷键打开麒麟百事通

    void preHandle();

    //global style
    void onComboBoxIndexChanged(int);

    void on_setPlayVisible_clicked();

    void on_setPlayInvisible_clicked();

    void on_setSmallFont_clicked();

    void on_setMiddleFont_clicked();

    void on_setBigFont_clicked();

    void on_generateButton_clicked();

    void on_changeCursor_clicked();

    void on_recoverCursor_clicked();

    void on_showTextArea_clicked();

    void on_showTextCancel_clicked();

    void checkSlot1(bool flag);
    void checkSlot2(bool flag);
    void checkSlot3(bool flag);
    void checkSlot4(bool flag);
    void checkSlot5(bool flag);

    void on_generateButton_listen_clicked();

    void on_rememberSettings_clicked();

    void on_defaultSettings_clicked();

    void on_transToBaidu_clicked();

    void on_startAi_clicked();

    //audio-recognize
    void audioRecorder();

    QString audioToText();

    void stopRecorder();

    void speakAiAnswer();

    void on_audioRecognize_pressed();

    void on_audioRecognize_released();

    void on_copyText_clicked();

    void audioAssistant();

    void startAssistant();

    void stopTimer();

    //连接自定义的信号的槽函数
    void call();
    void changeThemeToBw();
    void stopAi();
    void myCheckSlot1();
    void myCheckSlot2();
    void myCheckSlot3();
    void myCheckSlot4();
    void myCheckSlot5();

    void on_stopAi_clicked();

    void on_startAssistantLogo_clicked();

    void on_stopAssisantLogo_clicked();

    void on_bigWindow_clicked();

    void on_transToMooc_clicked();

    void on_transToQimingxing_clicked();

    void on_transToHaoyanli_clicked();

    void on_audioDate_clicked();

    void on_audioWeekday_clicked();

    void on_audioTime_clicked();

    void on_audioWeather_clicked();

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;

    void mousePressEvent(QMouseEvent *event) override;

    void mouseMoveEvent(QMouseEvent *event) override;

    void paintEvent(QPaintEvent *event) override;

private:
    Ui::MainWindow *ui;
    QPoint lastMousePosition;
    int nowTheme;
    // timer
    QTimer* posTimer;
    QTimer* ocrTimer;
    QTimer* preHandleTimer;
    // cutimage
    CutImage *cutimage;
    AiDialog *aidialog;
    double tolerance;
    QPoint nowPos;
    // ocr
    PyObject* pModule1;
    PyObject* pModule2;
    PyObject* pDeteImport;
    PyObject* pRecoImport;
    PyObject* pDeteOcr;
    PyObject* pRecoOcr;
    // tts
    PyObject* tts;
    PyObject* pyttsx3;
    PyObject* gtts;
    PyObject* speak_microsoft;
    PyObject* speak_gtts;
    PyObject* speak_pyttsx3;
    PyObject* changeText;
    PyObject* changeRate;
    PyObject* changePitch;
    PyObject* changeVoice;
    PyObject* setPyttsx3;
    PyObject* generateMp3;
    PyObject* speak_baidu;
    // audio
    PyObject* audio;
    PyObject* pAudioImport;
    PyObject* spark;
    PyObject* aiResponse;
    PyObject* pAudioRecognize;
    PyObject* currentDate;
    PyObject* currentWeekday;
    PyObject* currentTime;
    PyObject* currentWeather;
    // 录音
    QAudioRecorder* recorder;
    CustomDialog* dialog;
    AudioLogo* logo;
    QTimer* audioTimer;
    // 放大镜
    qreal zoom;
    MagnifierWindow* magnifier; // 放大镜窗口对象
    bool isMagnifierEnabled;   // 放大镜是否已经开启
    // 文本框
    ShowTextWindow* textwindow; // 文本框窗口对象
    bool isTextWindowEnabled;
    // QHotkeys
    QHotkey* magnifierKey;
    QHotkey* normalizeKey;
    QHotkey* startKey;
    QHotkey* quitKey;
    QHotkey* showtextfierKey;
    QHotkey* copyKey;
    QHotkey* AiKey;
    // 字体大小
    int fontSize;
    // 现在正在识别的文字，用来复制用
    QString nowText;
    // 是否开启语音提示
    int enableAudioTips;
    QVector<int> temptVector;
    QVector<int> textVector;
    QMediaPlayer* player;
    //aliyun tts
    AliyunTts* aliyuntts;
    QString audioPath;
    //the mode of tts
    int ttsMode;
    QList<QLabel *> audioLogoList;
};
#endif // MAINWINDOW_H
